﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
using QLKH.PopUp.Sudung;

namespace QLKH.View.Sudung
{
    public partial class Nhapso : UserControl
    {
        DataView view = new DataView();
        public Nhapso()
        {
            InitializeComponent();
        }
        private void bintreeview()
        {
            // add duong
            clDuong cld = new clDuong();
            DataTable dtduong = cld.getdata(1);
            if (dtduong != null && dtduong.Rows.Count > 0)
            {
                TreeNode rootNode = new TreeNode();
                rootNode.Text = "Danh sách đường";
                rootNode.Tag = "D-0";
                treeViewData.Nodes.Add(rootNode);
                TreeNode rootItem = null;
                for (int i = 0; i < dtduong.Rows.Count; i++)
                {
                    rootItem = new TreeNode();
                    rootItem.Text = dtduong.Rows[i]["tenDuong"].ToString();
                    rootItem.Tag = "D-" + dtduong.Rows[i]["id"].ToString();
                    rootNode.Nodes.Add(rootItem);
                }
            }
            // dong ho tong
            clDHTong cldht = new clDHTong();
            DataTable dtdht = cldht.getdata(1);
            if (dtdht != null && dtdht.Rows.Count > 0)
            {
                TreeNode rootNode = new TreeNode();
                rootNode.Text = "Đồng hồ tổng";
                rootNode.Tag = "DHT-0";
                treeViewData.Nodes.Add(rootNode);
                TreeNode rootItem = null;
                for (int i = 0; i < dtdht.Rows.Count; i++)
                {
                    rootItem = new TreeNode();
                    rootItem.Text = dtdht.Rows[i]["tenDHTong"].ToString();
                    rootItem.Tag = "DHT-" + dtdht.Rows[i]["id"].ToString();
                    rootNode.Nodes.Add(rootItem);
                }
            }
        }

        private void Nhapso_Load(object sender, EventArgs e)
        {
            clSudung clsd = new clSudung();
            DataTable dtsd = clsd.dsNhapso();

            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtsd.DefaultView;

            bintreeview();
        }

        public void Search(string id, string type)
        {   // khai báo 1 hàm với 2 tham số đầu vào là str1, và str2 nó sẽ đưa dữ liệu vào 2 lable
            // bin du lieu
            //MessageBox.Show(txtsearch + "-" + idd);
            string querySearch = "1=1";
            if (type == "D")
            {
                //querySearch += " and idDHTong = 0";
                if (Int16.Parse(id) > 0)
                    querySearch += " and idDuong = " + id;
                else
                    querySearch += " and idDuong > 0";
            }
            else
            {
                if (Int16.Parse(id) > 0)
                    querySearch += " and idDHTong = " + id;
                else
                    querySearch += " and idDHTong > 0";
            }
            view.RowFilter = querySearch;
            lbrowindex.Text = "-1";
        }

        public void Nhapsodh(int id)
        {
            clSudung clsd = new clSudung();
            DataTable dtsd = clsd.dsNhapso();

            dataGrid.AutoGenerateColumns = false;
            dataGrid.DataSource = view = dtsd.DefaultView;
        }

        private void tool_add_Click(object sender, EventArgs e)
        {
            popNhapso usfritem = new popNhapso();
            // ké thằng GetValue của form1 vào form2
            usfritem.MyGetData = new popNhapso.Nhapso(Nhapsodh);
            usfritem.ShowDialog();
        }

        public void TimKiem(string txtsearch, string type, Int16 id)
        {
            string querySearch = "1=1";
            if (txtsearch.Length > 0)
                querySearch += " and (maCu LIKE '%" + txtsearch + "%')";
            if (id > 0)
            {
                if (type == "D")
                    querySearch += " and idDuong = " + id + " and idDHTong = 0";
                else
                    querySearch += " and idDHTong = " + id;
            }
            view.RowFilter = querySearch;
        }

        private void tool_search_Click(object sender, EventArgs e)
        {
            popTimkiem usfritem = new popTimkiem();
            // ké thằng GetValue của form1 vào form2
            usfritem.MyGetData = new popTimkiem.TimKiem(TimKiem);
            usfritem.ShowDialog();
        }

        private void treeViewData_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string value = string.Empty;
            value = e.Node.Tag.ToString().Trim();
            if (value.Length > 0)
            {
                Search(value.Split('-')[1].Trim(), value.Split('-')[0].Trim());
            }
        }
    }
}
